/**
 * 
 */
package com.xbang.attendance.entity.role;

import java.io.Serializable;
import java.util.Map;
import java.util.Set;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Transient;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;

/**
 * 权限
 * 
 * @author dev.zhang
 */
@Entity
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class Role implements Serializable {

	private static final long serialVersionUID = 9042559288944464964L;

	@Id
	@GeneratedValue
	private Integer id;

	/** 角色名称 */
	private String name;

	/** 角色描述 */
	private String description;

	/** 角色所拥有的资源 */
	@ManyToMany(targetEntity = Resource.class, fetch = FetchType.EAGER)
	@JoinTable(name = "role_resource", joinColumns = @JoinColumn(name = "role_id"), inverseJoinColumns = @JoinColumn(name = "resource_id"))
	@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
	private Set<Resource> resources;

	@Transient
	private Map<String, Role> roles;

	public boolean isSelect() {
		if (roles == null || roles.isEmpty()) {
			return false;
		}

		return roles.containsKey(name);
	}

	/**
	 * The default constructor
	 */
	public Role() {

	}

	/**
	 * @return the id
	 */
	public Integer getId() {
		return id;
	}

	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * @return the resources
	 */
	public Set<Resource> getResources() {
		return resources;
	}

	/**
	 * @param id
	 *            the id to set
	 */
	public void setId(Integer id) {
		this.id = id;
	}

	/**
	 * @param name
	 *            the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * @param resources
	 *            the resources to set
	 */
	public void setResources(Set<Resource> resources) {
		this.resources = resources;
	}

	/**
	 * @return the description
	 */
	public String getDescription() {
		return description;
	}

	/**
	 * @param description
	 *            the description to set
	 */
	public void setDescription(String description) {
		this.description = description;
	}

	/**
	 * @return the roles
	 */
	public Map<String, Role> getRoles() {
		return roles;
	}

	/**
	 * @param roles
	 *            the roles to set
	 */
	public void setRoles(Map<String, Role> roles) {
		this.roles = roles;
	}

}
